/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mytdev.application.service.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;

import com.mytdev.application.service.AbstractServiceProvider;
import com.mytdev.application.service.ServiceException;
import java.sql.SQLException;

public final class DefaultJDBCConnectionProvider extends AbstractServiceProvider<Connection> implements JDBCConnectionProvider {

    public static final String DRIVER_PROPERTY = "driver";
    public static final String URL_PROPERTY = "url";
    public static final String USER_PROPERTY = "user";
    public static final String PASSWORD_PROPERTY = "password";

    @Override
    public Connection getService() throws ServiceException {
        Connection connection = null;
        try {
            Driver driver = (Driver) Class.forName(getDriver()).newInstance();
            DriverManager.registerDriver(driver);
            String url = getUrl();
            String user = getUser();
            String password = getPassword();

            Properties properties = new Properties();
            properties.putAll(getProperties());
            properties.remove(DRIVER_PROPERTY);
            properties.remove(URL_PROPERTY);
            properties.remove(USER_PROPERTY);
            properties.remove(PASSWORD_PROPERTY);
            if (properties.isEmpty()) {
                if ((user == null) && (password == null)) {
                    connection = DriverManager.getConnection(url);
                } else {
                    connection = DriverManager.getConnection(url, user, password);
                }
            } else {
                connection = DriverManager.getConnection(url, properties);
            }
        } catch (SQLException ex) {
            throw new ServiceException("could not connect to the database: " + ex.getLocalizedMessage(), ex);
        } catch (ClassNotFoundException ex) {
            throw new ServiceException("driver not found: " + ex.getLocalizedMessage(), ex);
        } catch (InstantiationException ex) {
            throw new ServiceException("could not load driver", ex);
        } catch (IllegalAccessException ex) {
            throw new ServiceException("could not load driver", ex);
        }
        return connection;
    }

    public String getDriver() {
        return (String) getProperty(DRIVER_PROPERTY);
    }

    public String getPassword() {
        return (String) getProperty(PASSWORD_PROPERTY);
    }

    public String getUrl() {
        return (String) getProperty(URL_PROPERTY);
    }

    public String getUser() {
        return (String) getProperty(USER_PROPERTY);
    }

    public void setDriver(String driver) {
        setProperty(DRIVER_PROPERTY, driver);
    }

    public void setPassword(String password) {
        setProperty(PASSWORD_PROPERTY, password);
    }

    public void setUrl(String url) {
        setProperty(URL_PROPERTY, url);
    }

    public void setUser(String user) {
        setProperty(USER_PROPERTY, user);
    }
}
